package com.exchange.web.home.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exchange.annotation.Auth;
import com.exchange.utils.ResultUtils;
import com.exchange.utils.ResultVo;
import com.exchange.web.goods.entity.Goods;
import com.exchange.web.goods.service.GoodsService;
import com.exchange.web.goods_report.entity.GoodsReport;
import com.exchange.web.goods_report.service.GoodsReportService;
import com.exchange.web.home.entity.vo.TotalVo;
import com.exchange.web.home.entity.vo.ChartVO;
import com.exchange.web.wx_user.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RequestMapping("/api/home")
@RestController
public class HomeController {
    @Autowired
    private GoodsReportService goodsReportService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private GoodsService goodsService;

    @Auth
    @GetMapping("/getTotal")
    public ResultVo getTotal() {
        TotalVo vo = new TotalVo();
        //查询待处理数量
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getStatus, "0");
        int doTotal = goodsReportService.count(query);
        vo.setDoTotal(doTotal);
        //会员总数
        int userCount = wxUserService.count();
        vo.setUserTotal(userCount);
        //闲置总数
        QueryWrapper<Goods> uquery = new QueryWrapper<>();
        uquery.lambda().eq(Goods::getType, "0");
        int uncount = goodsService.count(uquery);
        vo.setUnusedTotal(uncount);
        //求购总数
        QueryWrapper<Goods> buyquery = new QueryWrapper<>();
        buyquery.lambda().eq(Goods::getType, "1");
        int buycount = goodsService.count(buyquery);
        vo.setBuyTotal(buycount);
        return ResultUtils.success("查询成功", vo);
    }

    //查询投诉列表
    @Auth
    @GetMapping("/getDoReport")
    public ResultVo getDoReport() {
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getStatus, "0")
                .orderByDesc(GoodsReport::getReportTime)
                .last(" limit 8");
        List<GoodsReport> list = goodsReportService.list(query);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Goods goods = goodsService.getById(list.get(i).getGoodsId());
                if (goods != null) {
                    list.get(i).setGoodsName(goods.getGoodsName());
                }

            }
        }
        return ResultUtils.success("查询成功", list);
    }

    /**
     * @param DataSign 时间标识，0代表按当前星期，1代表当年每月
     * @return
     */
    @GetMapping("/getGoodsCharts")
    public ResultVo getCharts(String DataSign) {
        if ("0".equals(DataSign)) {//按当前星期
            List<ChartVO> list = goodsService.getChartsByWeek();
            return ResultUtils.success("查询成功", list);
        } else if ("1".equals(DataSign)) {//按当前年份
            List<ChartVO> list = goodsService.getChartsByYear();
            return ResultUtils.success("查询成功", list);
        }
        return null;
    }

}
